import Local_Window_BattleItemCategory from './windowItemCategory.js'
let SWDA_battleItem = {};

// 重定义 Window_BattleItem
//-----------------------------------------------------------------------------
// Window_BattleItem
//
// The window for selecting an item to use on the battle screen.
function Window_BattleItem() {
    this.initialize.apply(this, arguments);
}

Window_BattleItem.prototype = Object.create(Window_ItemList.prototype);
Window_BattleItem.prototype.constructor = Window_BattleItem;

Window_BattleItem.prototype.initialize = function (x, y, width, height) {
    Window_ItemList.prototype.initialize.call(this, x, y, width, height);
    this._backgroundSprite = new Sprite();
    this._backgroundSprite.bitmap =
        ImageManager.loadBitmap('img/system/UI/', 'MenuBg_List');
    this.addChildAt(this._backgroundSprite, 0);
    this._backgroundSprite.x = -17;
    this._backgroundSprite.y = -1;

    this._actor = null;
    this.hide();
};

SWDA_battleItem.Window_BattleItem_includes = Window_BattleItem.prototype.includes;
Window_BattleItem.prototype.includes = function (item) {
    var result = SWDA_battleItem.Window_BattleItem_includes.call(this, item);
    //去掉无法召唤的护驾
    // party canUse，为true时，说明只要有任意角色可以使用，则收录进列表里。为false时，说明所有角色都无法使用，则隐藏。
    result =  result && $gameParty.canUse(item);
    return result;
};
SWDA_battleItem.Window_BattleItem_isEnabled = Window_BattleItem.prototype.isEnabled;
Window_BattleItem.prototype.isEnabled = function(item) {
    let result = SWDA_battleItem.Window_BattleItem_isEnabled.call(this,item)
    if (this._actor)
        result = result && this._actor.canUse(item);
    return result;
    //return $gameParty.canUse(item);
};

Window_BattleItem.prototype.setActor = function(actor) {
    if (this._actor !== actor) {
        this._actor = actor;
        this.refresh();
        this.resetScroll();
    }
};


Window_BattleItem.prototype.show = function () {
    this.selectLast();
    this.showHelpWindow();
    Window_ItemList.prototype.show.call(this);
    BattleManager.requestElementSpritesUpdateAll();
};

Window_BattleItem.prototype.hide = function () {
    this.hideHelpWindow();
    Window_ItemList.prototype.hide.call(this);
    BattleManager.requestElementSpritesUpdateAll();
};

Window_BattleItem.prototype.maxCols = function () {
    return 1;
};

//=============================================================================
// Scene_Battle
//=============================================================================


// 顶部分类菜单
Scene_Battle.prototype.createItemCategoryWindow = function () {
    this._itemCategoryWindow = new Local_Window_BattleItemCategory();
    this._itemCategoryWindow.opacity = 0;
    // 分类窗口响应有问题
    //this._itemCategoryWindow.setHandler('ok',     this.onItemCategoryOk.bind(this));
    this._itemCategoryWindow.$on('select', this.onItemCategoryOk.bind(this));
    this._itemCategoryWindow.hide();
    this._itemCategoryWindow.deactivate();
    this.addWindow(this._itemCategoryWindow);


    // 设置外观基础值
    const [x, y, width] = [650, 40, 500];
    this._itemCategoryWindow.x = x
    this._itemCategoryWindow.y = y
    this._itemCategoryWindow.width = width

    // 关联行为
    this._itemCategoryWindow.filterRules = (item) => !['Equips', 'Weapons', 'Armors', 'Category:其他'].includes(item.trim())
    this._itemCategoryWindow.refresh()
};


Scene_Battle.prototype.onItemCategoryOk = function () {
    if (this._itemWindow) {
        this._itemWindow.selectLast();
    }
    this._itemCategoryWindow.refresh();
};

Scene_Battle.prototype.createItemWindow = function () {
    this.createItemCategoryWindow();//在创建物品窗口前创建物品分类窗口
    const [x, y, width, height] = [680, 105, 312, 480];
    this._itemWindow = new Window_BattleItem(x, y, width, height);
    this._itemWindow.setHelpWindow(this._helpWindow);
    this._itemWindow.width = 482
    this._itemWindow.opacity = 0
    this._itemWindow._scrollbar.setOffset(5)
    this._itemCategoryWindow.setItemWindow(this._itemWindow);
    this._itemWindow.setHandler('ok', this.onItemOk.bind(this));
    this._itemWindow.setHandler('cancel', this.onItemCancel.bind(this));
    this._itemWindow.$on('select', this.updateItemPreviewWindow.bind(this));
    this._itemWindow.$on('deselect', this.hideItemPreviewBox.bind(this));
    this.addWindow(this._itemWindow);
    
};


Scene_Battle.prototype.updateItemPreviewWindow = function (id) {
    if (!this._itemWindow) return;
    this._itemWindow.showView();
    this._itemWindow.showPetInfo();
}

Scene_Battle.prototype.hideItemPreviewBox = function () {
    if (!this._itemWindow) return;
    this._itemWindow.hideView();
    this._itemWindow.hidePetInfo();
}

// 选择物品时，隐藏所有物品相关窗口
SWDA_battleItem.Scene_Battle_onItemOk = Scene_Battle.prototype.onItemOk;
Scene_Battle.prototype.onItemOk = function () {
    SWDA_battleItem.Scene_Battle_onItemOk.call(this);
    this._itemCategoryWindow.hide();
    this._itemCategoryWindow.deactivate();
    this._itemWindow.hideView();
    this._itemWindow.hidePetInfo();
};

// 取消物品选择时，隐藏所有物品相关窗口
SWDA_battleItem.Scene_Battle_onItemCancel = Scene_Battle.prototype.onItemCancel;
Scene_Battle.prototype.onItemCancel = function () {
    SWDA_battleItem.Scene_Battle_onItemCancel.call(this);
    this._itemCategoryWindow.hide();
    this._itemCategoryWindow.deactivate();
    this._itemWindow.hideView();
    this._itemWindow.hidePetInfo();
};

// 取消目标选择时，恢复所有物品相关窗口
SWDA_battleItem.Scene_Battle_onActorCancel = Scene_Battle.prototype.onActorCancel;
Scene_Battle.prototype.onActorCancel = function () {
    SWDA_battleItem.Scene_Battle_onActorCancel.call(this);
    switch (this._actorCommandWindow.currentSymbol()) {
        case 'item':
            this._itemCategoryWindow.show();
            this._itemCategoryWindow.activate();
            this._itemCategoryWindow.selectLast();
            this._itemWindow.showView();
            this._itemWindow.showPetInfo();
            break;
    }
};

SWDA_battleItem.Scene_Battle_onEnemyCancel = Scene_Battle.prototype.onEnemyCancel;
Scene_Battle.prototype.onEnemyCancel = function () {
    SWDA_battleItem.Scene_Battle_onEnemyCancel.call(this);
    switch (this._actorCommandWindow.currentSymbol()) {
        case 'item':
            this._itemCategoryWindow.show();
            this._itemCategoryWindow.activate();
            this._itemCategoryWindow.selectLast();
            this._itemWindow.showView();
            this._itemWindow.showPetInfo();
            break;
    }
};

// 选择物品指令时，显示所有物品相关窗口
SWDA_battleItem.SceneBattle_commandItem = Scene_Battle.prototype.commandItem;
Scene_Battle.prototype.commandItem = function () {
    this._itemWindow.setActor(BattleManager.actor());
    SWDA_battleItem.SceneBattle_commandItem.call(this);
    this._itemCategoryWindow.show();
    this._itemCategoryWindow.activate();
    this._itemCategoryWindow.selectLast();
};